package com.miyake.client.profile;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;
import com.miyake.client.tools.ClickableImage;
import com.miyake.client.tools.ImageListElement;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;

public class FaceListDialog extends DialogBox {
	protected DoctorsUserServiceAsync service = DoctorsUserService.Util.getInstance();

	public FaceListDialog(String key) {
		setHTML("Images");
		
		VerticalPanel verticalPanel = new VerticalPanel();
		setWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");
		
		getFaceList(key, verticalPanel);
	}

	private void getFaceList(final String key, final VerticalPanel verticalPanel) {
		service.getFaceImages(key, new AsyncCallback<List<ImageListElement>>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<ImageListElement> result) {
				showList(result, verticalPanel, key);
			}
			
		});
	}

	protected void showList(List<ImageListElement> result,
			VerticalPanel verticalPanel, final String key) {
		final Iterator<ImageListElement> it = result.iterator();
		ThumbNail thumbNail = new ThumbNail(result.size(), 8) {
			@Override
			protected Widget getWidget(final int i) {
				return createElement(key, i, it.next());
			}
		};
		verticalPanel.add(thumbNail);
		thumbNail.show();
		verticalPanel.add(new SmallButton("Close", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide(false);
			}
		}));
	}
	
	private void selectImage(String key, int i) {
		service.setFaceImage(key, i, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(Void result) {
				hide(false);
			}
			
		});
	}

	private Widget createElement(final String key, final int i,
			final ImageListElement e) {
		VerticalPanel v = new VerticalPanel();
		Image image = new ClickableImage();
		image.setUrl(e.getImageURL());
		image.setSize("80px", "80px");
		image.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				selectImage(key, i);
			}
		});
		v.setCellHorizontalAlignment(image, HasHorizontalAlignment.ALIGN_CENTER);
		v.add(image);
		HorizontalPanel h = new HorizontalPanel();
		h.add(new SmallButton("Select", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				selectImage(key, i);
			}			
		}));
		h.add(new SmallButton("Remove", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				new ShowDialog() {
					@Override
					protected void onOK(String text) {
					}

					@Override
					protected DialogBox getDialogBox() {
						return new OkCancelDialog();
					}
				};
			}
		}));
		h.setWidth("100%");
		v.add(h);
		return v;
	}
}
